package ar.edu.unq.tpi.uis.tepe3.dominio;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/*
 * Existen carpetas, que forman una estructura arborea.
Hay una carpeta root, y debajo múltiples niveles de carpetas hijas.
El sistema debe permitir crear, borrar y editar carpetas.
 * */

public class Folder {
	
	//DB PROPERTIES
	private Long id;
	private String name;
	private Folder father;
	private Set<Folder> daughters;
	private Set<Privilege> privileges;
	
	//private List<Folder> ruta;

	public Folder(){
		super();
		this.daughters = new HashSet<Folder>();
		this.privileges = new HashSet<Privilege>();
	}
	
	
	public Folder(String aName) {
		this();
		this.name = aName;
	}

	public Folder(Folder elfather){
		super();
		this.setFather(elfather);
	}
	
	//*******************
	// GETTERS Y SETTERS
	//*******************

	public Long getId() {
		return id;
	}

	/**
	 * private!
	 * 
	 * @param idmodel
	 */
	private void setId(Long id) {
		this.id = id;
	}

	

	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public Folder getFather() {
		return father;
	}


	public void setFather(Folder father) {
		this.father = father;
	}


	public Set<Folder> getDaughters() {
		return daughters;
	}


	public void setDaughters(Set<Folder> daughters) {
		this.daughters = daughters;
	}


	public Set<Privilege> getPrivileges() {
		return privileges;
	}


	public void setPrivileges(Set<Privilege> privileges) {
		this.privileges = privileges;
	}


	/*metodos*/
	public void move(Folder destination){
		this.setFather(destination);
	}
	
	public Boolean isRoot(){
		return this.father == null;
	}
	
	public List<Folder> getRoute(){
		
		List<Folder> ret = new  ArrayList<Folder>();
		
		if (!this.isRoot()) {
			ret.addAll(this.father.getRoute());
		}
		
		ret.add(this);
		return ret;
	}
	
	public String getRouteString(){
		String retString = "/";
		for (Folder folder : this.getRoute()) {
			retString = retString.concat(folder.getName());
			retString = retString.concat("/");
		}
		return retString;
	}
	

	public void addDaughter(Folder aChild){
		this.daughters.add(aChild);
		aChild.setFather(this);
	}
	
	public String toString(){
		return this.getName();
	}
	
	public boolean equals(Folder folder) {
		return this.getId().equals(folder.getId());
	}
}
